# Refer: https://blog.csdn.net/weixin_44827418/article/details/111255414
import yaml
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

# smtplib模块主要负责发送邮件：是一个发送邮件的动作，连接邮箱服务器，登录邮箱，发送邮件（有发件人，收信人，邮件内容）。
# email模块主要负责构造邮件：指的是邮箱页面显示的一些构造，如发件人，收件人，主题，正文，附件等。
class Email:
    def __init__(self, mail_title: str = "WaveCat Log", mail_content: str = ""):
        """初始化邮箱发送业务 读取发收件信息

        Args:
            mail_title (str): 邮件标题 default "WaveCat Log"
            mail_content (str): 发送邮件内容 default ""
        """
        # 初始化用户信息
        with open("pwd.yaml", "r", encoding="utf-8") as file:
            self.info = yaml.safe_load(file)  # 使用 safe_load 避免潜在安全问题
        # 输出读取的数据
        self.host_server = self.info["host_server"]  # qq邮箱smtp服务器
        self.sender_email = self.info["sender_email"]  # 发件人邮箱
        self.pwd = "wjbvdjozxbadegfh"
        self.receiver = [self.info["receiver_email"]]  # 收件人邮箱
        self.mail_title = mail_title  # 邮件标题
        self.mail_content = mail_content  # 邮件正文内容

        # 初始化一个邮件主体
        self.msg = MIMEMultipart()
        self.msg["Subject"] = Header(mail_title, "utf-8")
        self.msg["From"] = self.sender_email
        self.msg["To"] = ";".join(self.receiver)
        # 邮件正文内容
        self.msg.attach(MIMEText(mail_content, "plain", "utf-8"))
    
    # 发送邮件
    def send(self):
        smtp = SMTP_SSL(self.host_server)  # ssl登录
        smtp.login(self.sender_email, self.pwd)
        smtp.sendmail(self.sender_email, self.receiver, self.msg.as_string())
        smtp.quit()
